package cc.mxtt.mvc.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import cc.mxtt.common.core.BaseController;
import cc.mxtt.common.core.GlobalKey;
import cc.mxtt.common.utils.DateUtil;
import cc.mxtt.common.utils.MD5Util;
import cc.mxtt.mvc.interceptor.UserLogInterceptor;
import cc.mxtt.mvc.model.Dept;
import cc.mxtt.mvc.model.Role;
import cc.mxtt.mvc.model.RoleMenu;
import cc.mxtt.mvc.model.User;
import cc.mxtt.mvc.model.UserRole;
import cc.mxtt.mvc.service.UserService;
import cc.mxtt.mvc.service.impl.UserServiceImpl;
import com.jfinal.aop.Before;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Page;

public class UserController extends BaseController{
	UserService userService = new UserServiceImpl();
	public void index(){
		render("index.jsp");
	}
	public void list(){
		int pageNumber = getParaToInt("page",1);
		int pageSize = getParaToInt("rows",10);
		String username = getPara(0,""); //搜索条件，如果没有则为空
		try {
			username = URLDecoder.decode(username, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		User user = getSessionAttr(GlobalKey.CurrentUser);
		Long deptId = user.getLong("dept_id");
		Long userId = user.getLong("id");
		
		UserRole menu = UserRole.dao.findFirst("select * from sys_user_role where user_id =?" , userId);
		Role role = Role.dao.findFirst("select * from sys_role where id =?", menu.getLong("role_id"));
		String roleName = role.getStr("name");
		
		String sql = "select t.id,t.name,t.description,t.parent_id,t.order_id from sys_dept t where t.id=?";
		Dept dept = Dept.dao.findFirst(sql,deptId);
		Long id = dept.getLong("parent_id");
		Page<User> listuser =null;
        if(roleName.equals("系统管理员")){
        	listuser= userService.getDataGrid(pageNumber, pageSize, username );
		}else{
			listuser= userService.getDataGrid(pageNumber, pageSize, username ,Long.toString(id));
		}
		 
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", listuser.getTotalRow());
		result.put("rows", listuser.getList());
		System.out.println("user:"+JsonKit.toJson(result).toString());
		renderJson(JsonKit.toJson(result));
	}
	@Before(UserLogInterceptor.class)
	public void add(){
		User user = new User();
		user.set("username", getPara("username"));
		user.set("password", MD5Util.md5("111111"));//添加用户时，赋予默认密码为6个1
		user.set("nickname", getPara("nickname"));
		user.set("zuoji", getPara("zuoji"));
		user.set("shouji", getPara("shouji"));
		user.set("mail", getPara("mail"));
		user.set("idcard", getPara("idcard"));
		
		user.set("sex", getPara("sex"));
		user.set("dept_id",	getPara("dept_id"));
		user.set("status", getPara("status"));
		user.set("updatetime", DateUtil.getDateTime(new Date()));
		Map<String, Boolean> result = new HashMap<String, Boolean>();
		if (userService.create(user)){
			result.put("flag", true);
		}else{
			result.put("flag", false);
		}
		renderJson(JsonKit.toJson(result));
	}
	@Before(UserLogInterceptor.class)
	public void edit(){
		User user = userService.findById(getPara("id"));
		user.set("username", getPara("username"));
		//user.set("password", MD5Util.md5("111111"));//添加用户时，赋予默认密码为6个1
		user.set("nickname", getPara("nickname"));
		user.set("zuoji", getPara("zuoji"));
		user.set("shouji", getPara("shouji"));
		user.set("dept_id",	getPara("dept_id"));
		user.set("status", getPara("status"));
		user.set("updatetime", DateUtil.getDateTime(new Date()));
		Map<String, Boolean> result = new HashMap<String, Boolean>();
		if(userService.update(user)){
			result.put("flag", true);
		}else{
			result.put("flag", false);
		}
		renderJson(JsonKit.toJson(result));
	}
	@Before(UserLogInterceptor.class)
	public void delete(){
		Map<String, Object> result = new HashMap<String, Object>();
		boolean flag = true;
		String Msg = "";
		String delIds = getPara("delIds");
		if(userService.deleteUserRoleByUserId(delIds)>0){
			Msg = "用户与角色的关联信息删除成功,";
		}
		if (userService.delete(delIds)>0){
			Msg = Msg + "用户的基本信息删除成功！";
		}else{
			flag = false;
			Msg = Msg + "用户的基本信息删除失败！";
		}
		result.put("flag", flag);
		result.put("Msg", Msg);
		renderJson(JsonKit.toJson(result));
	}
	//重置密码
	public void resetpassword(){
		Map<String, Object> result = new HashMap<String, Object>();
		boolean flag = true;
		String Msg = "";
		User user = userService.findById(getPara("id"));
		user.set("password", MD5Util.md5("111111"));
		user.set("updatetime", DateUtil.getDateTime(new Date()));
		if(userService.update(user)){
			Msg = "用户密码已重置为：6个1。";
		}else{
			flag = false;
			Msg = "用户密码重置失败！";
		}
		result.put("flag", flag);
		result.put("Msg", Msg);
		renderJson(JsonKit.toJson(result));
	}
	
	public void lock(){
		System.out.println("lock");
		Map<String, Object> result = new HashMap<String, Object>();
		boolean flag = true;
		String Msg = "";
		User user = userService.findById(getPara("id"));
		int lock = user.getInt("lock");
		System.out.println("lock："+lock);
		if(lock == 0){//未锁定
			user.set("lock", 1);
			user.set("updatetime", DateUtil.getDateTime(new Date()));
		}else{
			Msg = "用户已锁定";
			flag = false;
		}
		
		if(userService.update(user) && flag){
			Msg = "用户锁定成功"; 
		}else{
			Msg = "用户锁定失败";
			flag = false;
		}
		result.put("flag", flag);
		result.put("Msg", Msg);
		renderJson(JsonKit.toJson(result));
		
	}
	
	public void unLock(){
		System.out.println("unlock");
		Map<String, Object> result = new HashMap<String, Object>();
		boolean flag = true;
		String Msg = "";
		User user = userService.findById(getPara("id"));
		int lock = user.getInt("lock");
		if(lock == 1){//已锁定
			user.set("lock", 0);
			user.set("updatetime", DateUtil.getDateTime(new Date()));
		}else{
			Msg = "用户未锁定";
			flag = false;
		}
		
		if(userService.update(user)){
			Msg = "用户解锁成功";
		}else{
			Msg = "用户解锁失败";
			flag = false;
		}
		result.put("flag", flag);
		result.put("Msg", Msg);
		renderJson(JsonKit.toJson(result));
		
	}
	public void roleAssign(){
		Map<String, Object> result = new HashMap<String, Object>();
		boolean flag = true;
		String Msg = "";
		String userid = getPara("userId");
		String roleids = getPara("roleIds");
		if(userService.addUserRoleByUserIdRoleIds(userid, roleids)){
			Msg = "为用户分配角色成功!";
		}else{
			flag = false;
			Msg = "为用户分配角色失败，请重新分配！";
		}
		result.put("flag", flag);
		result.put("Msg", Msg);
		renderJson(JsonKit.toJson(result));
	}
	public void listRole(){
		String userid = getPara(0);
		renderJson(JsonKit.toJson(userService.getListRoleByUserId(userid)));
	}
}
